package com.zeus.core.report;

import com.zeus.common.enums.ZeusEventTypeEnum;
import com.zeus.common.utils.DateUtils;
import com.zeus.common.utils.MarkdownUtils;
import com.zeus.core.config.data.ZeusConfig;
import com.zeus.core.context.ZeusContext;
import com.zeus.core.open.config.ZeusInfo;

import java.util.List;

/**
 * 简单报告抽象类
 *
 * @author f1ybear_wang  2022/3/2 5:00 下午
 */
public abstract class AbstractSimpleZeusReportClient extends AbstractZeusReportClient {
    /**
     * 消息内容
     */
    protected String message;

    public AbstractSimpleZeusReportClient() {
        super();
    }

    @Override
    protected String workDingTalkMessage() {
        // 组装提醒内容字符串
        StringBuilder sb = new StringBuilder();
        // 标题
        sb.append(getTitle());

        // 加工基本指标
        sb.append(MarkdownUtils.getItem("时间", DateUtils.getSimpleString(this.occurrenceTime)));
        sb.append(MarkdownUtils.getItem("ELK", getElkUrl()));
        sb.append(MarkdownUtils.getItem("消息", message));
        sb.append(MarkdownUtils.getItem("部署Tag", ZeusContext.getContext().getDeployTag()));
        sb.append(MarkdownUtils.getItem("服务器IP", ZeusContext.getContext().getHostIp()));
        sb.append(MarkdownUtils.getItem("服务器名称", ZeusContext.getContext().getHostName()));
        sb.append(MarkdownUtils.getItem("宙斯版本", ZeusInfo.getVersion()));
        return sb.toString();
    }

    @Override
    public void complete() {
        // 添加开关判断，增强控制
        if (!ZeusContext.getContext().alarmEnable()) {
            return;
        }

        // 发送
        sendReport();
    }

    @Override
    public boolean isCompleted() {
        return true;
    }

    @Override
    protected List<ZeusConfig.DingTalkInfo> getDingTalkList() {
        return ZeusContext.getContext().getBaseDingTalkList();
    }

    @Override
    protected void parsingExtend() {
        // default
    }

    @Override
    protected String getTitle() {
        ZeusEventTypeEnum eventType = eventType();

        String start = "【宙斯-" + this.model.getName() + "】";
        // 项目启动或关闭
        return MarkdownUtils.getTitle(start + ZeusContext.getContext().getAppId() + eventType.getDescription() , true);
    }
}
